!function ($) {
    let $sid = location.search.substring(1).split('=')[1];
    const $pull_left = $('#pull_left');
    const $smallpic = $('#smallpic');
    const $bpic = $('#bpic');
    const $title = $('.loadtitle');
    const $price = $('.loadpcp');

    if (!$sid) {
        $sid = 1;
    }
    $.ajax({
        url: 'http://localhost/Yoho/php/getsid.php',
        data: {
            sid: $sid
        },
        dataType: 'json'
    }).done(function (d) {
        console.log(d);
        $smallpic.attr('src', d.url);
        $smallpic.attr('sid', d.sid);
        $bpic.attr('sid', d.url);
        $title.html(d.title);
        $price.html(d.price);
        console.log(d.priclisturl.split(','));

        let picarr = d.priclisturl.split(',');
        let $strhtml = '';
        $.each(picarr, function (index, value) {
            $strhtml += '<li><img src="' + value + '" /></li>';
        });
        $('#list ul').html($strhtml);
    });

    // 放大镜
    const $spic = $('#spic');
    const $sf = $('#sf');
    const $bf = $('#bf');
    // const $spic = $('#spic');
    const $list = $('#list');

    // console.log($spic.width() , $bf.width() , $bpic.width());
    // console.log($spic.height() , $bf.height() , $bpic.height());
    $sf.width($spic.width() * $bf.width() / $bpic.width());
    $sf.height($spic.height() * $bf.height() / $bpic.height());
    let $bili = $bpic.width() / $spic.width();//比例大于1 放大效果

    $spic.hover(function () {
        $sf.css('visibility', 'visible');
        $bf.css('visibility', 'visible');
        $(this).on('mousemove',function(ev) {
            let $leftvalue = ev.pageX - $('.pull_left').offset().left - $sf.width() / 2;
            let $topvalue = ev.pageY - $('.pull_left').offset().top - $sf.height() / 2;
            if ($leftvalue < 0) {
                $leftvalue = 0;
            } else if ($leftvalue >= $spic.width() - $sf.width()) {
                $leftvalue = $spic.width() - $sf.width()
            }

            if ($topvalue < 0) {
                $topvalue = 0;
            } else if ($topvalue >= $spic.height() - $sf.height()) {
                $topvalue = $spic.height() - $sf.height()
            }

            $sf.css({
                left: $leftvalue,
                top: $topvalue
            });

            $bpic.css({
                left: -$leftvalue * $bili,
                top: -$topvalue * $bili
            });

        });
    }, function () {
        $sf.css('visibility', 'hidden');
        $bf.css('visibility', 'hidden');
    });


    // 存储数组
    let arrsid = [];
    let arrnum =[];
    function cookietoarray(){
        if(jscookie.get('cookiesid') && jscookie.get('cookienum')){
            arrsid = jscookie.get('cookiesid').split(',');
            arrnum = jscookie.get('cookienum').split(',');
        }else{
            arrsid =[];
            arrnum = [];
        }
    }

    $('.p-btn a').on('click',function(){
        alert(88888);
        let $sid = $(this).parents('.detail_box').find('#smallpic').attr('sid');
        cookietoarray();
        if($.inArray($sid,arrsid) != -1){
            let $num = parseInt(arrnum[$.inArray($sid, arrsid)]) + parseInt($('#count').val());
            arrnum[$.inArray($sid,arrsid)] = $num;
            jscookie.add('cookienum',arrnum, 10);
        }else{
            arrsid.push($sid);
            jscookie.add('cookiesid' ,arrsid, 10);
            arrnum.push($('#count').val());
            jscookie.add('cookienum',arrnum ,10);
        }
    });
}(jQuery);